// pages/shopManager/shopEmpower/shopEmpower.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        empowerType: 1,
        empowerList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getEmpowerList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getEmpowerList()
    },

    getEmpowerList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let data = {
            apply_type: this.data.empowerType,
            limit: 10,
            page: this.data.nowPage
        }
        let empowerList = this.data.empowerList
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Storequota/apply_store_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    empowerList: empowerList,
                    isAll: true
                })
            } else {
                empowerList = empowerList.concat(res)
                that.setData({
                    empowerList: empowerList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    navClick: function(e) {
        if (this.data.empowerType == e.currentTarget.dataset.type) {
            return
        }
        this.setData({
            empowerType: e.currentTarget.dataset.type,
            empowerList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        this.getEmpowerList()
    },

    goAgree: function(e) {
        const that = this
        wx.showLoading({
            title: '请稍后'
        })
        let data = {
            quota_apply_id: e.currentTarget.dataset.id,
            type: 1
        }
        common.post('Storequota/apply_store_check', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                return
            } else {
                wx.showModal({
                    title: '提示',
                    content: '通过成功',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            that.setData({
                                empowerList: [],
                                nowPage: 0,
                                isLoading: false,
                                isAll: false
                            })
                            that.getEmpowerList()
                        }
                    }
                })
            }
        }).catch(function(e) {})
    },

    goReject: function(e) {
        const that = this
        wx.showLoading({
            title: '请稍后'
        })
        let data = {
            quota_apply_id: e.currentTarget.dataset.id,
            type: 2
        }
        common.post('Storequota/apply_store_check', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                return
            } else {
                wx.showModal({
                    title: '提示',
                    content: '拒绝成功',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            that.setData({
                                empowerList: [],
                                nowPage: 0,
                                isLoading: false,
                                isAll: false
                            })
                            that.getEmpowerList()
                        }
                    }
                })
            }
        }).catch(function(e) {})
    },

    goCall: function(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone
        })
    }
})